package com.msmall.community.bbs.dao.hibernate;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.base.exception.DataNotFoundException;
import com.msmall.community.bbs.dao.MasterDao;
import com.msmall.community.bbs.model.Master;


public class MasterDaoHibernate extends BaseDaoHibernate<Master, Long> implements MasterDao{

	public MasterDaoHibernate(){
		super(Master.class);
	}
	
	public List<Master> findMasterByThreadId(Long threadId) throws DataNotFoundException {
		String queryString = "select m from Master m, Thread t, Topic p where t.threadId=? and t.topicid=p.topicid and p.boardid= m.boardId";
		try{
			List list = getHibernateTemplate().find(queryString, threadId);
			if(list == null || list.size() <= 0)
				return null;
			return list;
		}catch(Exception e){
			throw new DataNotFoundException(e);
		}
	}
	
	public List<Master> findMasterByTopicId(Long topicId) throws DataNotFoundException {
		String queryString = "select m from Master m, Topic t where t.boardid = m.boardId and t.topicId=?";
		try{
			List list = getHibernateTemplate().find(queryString,topicId);
			if(list == null || list.size() <= 0)
				return null;
			return list;
		}catch(Exception e){
			throw new DataNotFoundException(e);
		}
	}

	public List<Master> findMasterByBoardId(Long boardId) throws DataNotFoundException {
		String queryString = "from Master m where m.boardId=?";
		try{
			List list = getHibernateTemplate().find(queryString, boardId);
			if(list == null || list.size() <= 0)
				return null;
			return list;
		}catch(Exception e){
			throw new DataNotFoundException(e);
		}
	}

	public List<Master> findMasterByUserid(Long userid) throws DataNotFoundException {
		String queryString = "from Master m where m.userid=?";
		try{
			List list = getHibernateTemplate().find(queryString, userid);
			if(list == null || list.size() <= 0)
				return null;
			return list;
		}catch(Exception e){
			throw new DataNotFoundException(e);
		}
	}
	public Master findMaster(Long boardId, Long userid) throws DataNotFoundException{
		String queryString = "from Master m where m.userid=? and m.boardId=?";
		try{
			Object [] objs = new Object[2];
			objs[0] = userid;
			objs[1] = boardId;
			List list = getHibernateTemplate().find(queryString, objs);
			if(list == null || list.size() <= 0)
				return null;
			return (Master) list.get(0);
		}catch(Exception e){
			throw new DataNotFoundException(e);
		}
	}
}